package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.TbRoomDevice;
import com.ruoyi.system.mapper.TbRoomDeviceMapper;
import com.ruoyi.system.service.ITbRoomDeviceService;
import com.ruoyi.system.service.ITbRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 大屏统计接口Controller
 *
 * @author 朱欢乐
 * @date 2025-10-25
 */
@RestController
@RequestMapping("/dp")
public class DpController extends BaseController {
    @Autowired
    private ITbRoomService tbRoomService;


    @Autowired
    ITbRoomDeviceService tbRoomDeviceService;

    @Autowired
    TbRoomDeviceMapper tbRoomDeviceMapper;

    /**
     * 查询房间管理列表
     */
    @GetMapping("/onlineRoomNum")
    public JSONObject onlineRoomNum() {
        TbRoomDevice tbRoomDevice = new TbRoomDevice();
        List<TbRoomDevice> tbRoomDevices = tbRoomDeviceService.selectTbRoomDeviceList(tbRoomDevice);
        Integer onLineRoom = 0;
        Integer OnLineUserNum = 0;
        if (!CollectionUtils.isEmpty(tbRoomDevices)) {
            OnLineUserNum = tbRoomDevices.size();
            Map<Long, List<TbRoomDevice>> collect = tbRoomDevices.stream().collect(Collectors.groupingBy(TbRoomDevice::getRoomId));
            onLineRoom = collect.size();
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OnLineUserNum", OnLineUserNum);
        jsonObject.put("onLineRoom", onLineRoom);
        return jsonObject;
    }


    @GetMapping("/userCj")
    public JSONObject userCj(TbRoomDevice tbRoomDevice) {
        List<TbRoomDevice> tbRoomDevices = tbRoomDeviceService.selectTbRoomDeviceList(tbRoomDevice);
        Integer onLineRoom = 0;
        Integer OnLineUserNum = 0;
        if (!CollectionUtils.isEmpty(tbRoomDevices)) {
            OnLineUserNum = tbRoomDevices.size();
            Map<Long, List<TbRoomDevice>> collect = tbRoomDevices.stream().collect(Collectors.groupingBy(TbRoomDevice::getRoomId));
            onLineRoom = collect.size();
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OnLineUserNum", OnLineUserNum);
        jsonObject.put("onLineRoom", onLineRoom);
        return jsonObject;
    }


    @PostMapping("/sendCommandToDevice")
    public boolean sendCommandToDevice(@RequestBody JSONObject jsonObject) {
        return tbRoomDeviceService.sendCommandToDevice(jsonObject.getString("deviceId"), jsonObject.getJSONObject("cmd").toJSONString());
    }

}
